package com.zhkj.txg.module.home.adapter

import android.graphics.Paint
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.home.entity.SearchProductEntity
import com.zhkj.txg.utils.ImageUtils

class EventCommodityAdapter : BaseQuickAdapter<SearchProductEntity, BaseViewHolder>(R.layout.item_event_goods) {

    init {
        addChildClickViewIds(R.id.tvEventAddCart)
    }

    override fun convert(helper: BaseViewHolder, item: SearchProductEntity) {
//        val ivEventImage = helper.setText(R.id.tvEventName, item.goodsName)
//            .setText(R.id.tvEventPrice, SpanUtil.setMoneySymbol(item.price, false))
//            .getView<ImageView>(R.id.ivEventImage)
//        ImageUtils.loadImage(context, ivEventImage, item.image)
        //是否活动价
        val isActivePrice: Boolean = !item.activePrice.isNullOrEmpty()
        //是否会员价
        val isPlusPrice: Boolean = !item.plusPrice.isNullOrEmpty() && item.activePrice.isNullOrEmpty()

        val tvGoodsPrice: TextView = helper.getView(R.id.tvEventPrice)
        val tvGoodsPrice2: TextView = helper.getView(R.id.tvEventPrice2)
        val llGoodsPush: LinearLayout = helper.getView(R.id.llEventPush)
        val tvGoodsPushPrice: TextView = helper.getView(R.id.tvEventPushPrice)
        val tvGoodsDiscount: TextView = helper.getView(R.id.tvEventDiscount)

        if (isActivePrice) {
            tvGoodsPrice.visibility = View.VISIBLE
            tvGoodsPrice2.visibility = View.VISIBLE
            tvGoodsDiscount.visibility = View.VISIBLE
            llGoodsPush.visibility = View.GONE
            tvGoodsPrice.text = SpanUtil.setMoneySymbol(item.activePrice, true)
            tvGoodsPrice2.text = SpanUtil.setMoneySymbol(item.price, false)
            tvGoodsPrice2.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG
            tvGoodsDiscount.text = "${item.discount}折"
        } else if (isPlusPrice) {
            tvGoodsPrice.visibility = View.VISIBLE
            tvGoodsPrice2.visibility = View.GONE
            tvGoodsDiscount.visibility = View.GONE
            llGoodsPush.visibility = View.VISIBLE
            tvGoodsPrice.text = SpanUtil.setMoneySymbol(item.price, false)
            tvGoodsPushPrice.text = SpanUtil.setMoneySymbol(item.plusPrice, false)
        } else {
            tvGoodsPrice.visibility = View.VISIBLE
            tvGoodsPrice2.visibility = View.GONE
            tvGoodsDiscount.visibility = View.GONE
            llGoodsPush.visibility = View.GONE
            tvGoodsPrice.text = SpanUtil.setMoneySymbol(item.price, false)
        }
        val imageView = helper.setText(R.id.tvEventName, item.goodsName)
//            .setText(R.id.tvCateProductPrice, SpanUtil.setMoneySymbol(item.price, true))
//            .setText(R.id.tvProductSale, context.getString(R.string.sale_format, item.saleCount ?: "0"))
            .getView<ImageView>(R.id.ivEvent)
        helper.getView<ImageView>(R.id.ivEvent2).visibility = if (item.shopCount == 0) {
            View.VISIBLE
        } else {
            View.GONE
        }
        ImageUtils.loadRoundCornerImage(context, imageView, item.image)
    }
}


class RecommendCommodityAdapter : BaseQuickAdapter<SearchProductEntity, BaseViewHolder>(R.layout.item_cate_product) {

    override fun convert(helper: BaseViewHolder, item: SearchProductEntity) {
        //是否活动价
        val isActivePrice: Boolean = !item.activePrice.isNullOrEmpty()
        //是否会员价
        val isPlusPrice: Boolean = !item.plusPrice.isNullOrEmpty() && item.activePrice.isNullOrEmpty()

        val tvGoodsPrice: TextView = helper.getView(R.id.tvCateProductPrice)
        val tvGoodsPrice2: TextView = helper.getView(R.id.tvCateProductPrice2)
        val llGoodsPush: LinearLayout = helper.getView(R.id.llCateProductPush)
        val tvGoodsPushPrice: TextView = helper.getView(R.id.tvCateProductPushPrice)
        val tvGoodsDiscount: TextView = helper.getView(R.id.tvCateProductDiscount)

        if (isActivePrice) {
            tvGoodsPrice.visibility = View.VISIBLE
            tvGoodsPrice2.visibility = View.VISIBLE
            tvGoodsDiscount.visibility = View.VISIBLE
            llGoodsPush.visibility = View.GONE
            tvGoodsPrice.text = SpanUtil.setMoneySymbol(item.activePrice, true)
            tvGoodsPrice2.text = SpanUtil.setMoneySymbol(item.price, false)
            tvGoodsPrice2.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG
            tvGoodsDiscount.text = "${item.discount}折"
        } else if (isPlusPrice) {
            tvGoodsPrice.visibility = View.VISIBLE
            tvGoodsPrice2.visibility = View.GONE
            tvGoodsDiscount.visibility = View.GONE
            llGoodsPush.visibility = View.VISIBLE
            tvGoodsPrice.text = SpanUtil.setMoneySymbol(item.price, false)
            tvGoodsPushPrice.text = SpanUtil.setMoneySymbol(item.plusPrice, false)
        } else {
            tvGoodsPrice.visibility = View.VISIBLE
            tvGoodsPrice2.visibility = View.GONE
            tvGoodsDiscount.visibility = View.GONE
            llGoodsPush.visibility = View.GONE
            tvGoodsPrice.text = SpanUtil.setMoneySymbol(item.price, false)
        }
        val imageView = helper.setText(R.id.tvCateProductName, item.goodsName)
//            .setText(R.id.tvCateProductPrice, SpanUtil.setMoneySymbol(item.price, true))
//            .setText(R.id.tvProductSale, context.getString(R.string.sale_format, item.saleCount ?: "0"))
            .getView<ImageView>(R.id.ivCateProduct)
        helper.getView<ImageView>(R.id.ivCateProduct2).visibility = if (item.shopCount == 0) {
            View.VISIBLE
        } else {
            View.GONE
        }
        ImageUtils.loadRoundCornerImage(context, imageView, item.image)
    }
}